package com.classtwo.colleage.service.iml;

import com.classtwo.colleage.conf.DatabaseStudentInfo;
import com.classtwo.colleage.conf.DatabaseUser_classInfo;
import com.classtwo.colleage.model.Student;
import com.classtwo.colleage.model.User_class;
import com.classtwo.colleage.service.IFileToExcelService;
import com.classtwo.colleage.tool.ExcelHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class IFileToExcelServiceImpl implements IFileToExcelService {
    @Autowired
    private DatabaseStudentInfo studentInfo;
    @Autowired
    private DatabaseUser_classInfo classInfo;
    @Override
    public List<User_class> importExcel(String path,String classnodeUpload,String classnameUpload) {
        String classnode = classnodeUpload;
        String classname = classnameUpload;
        List<User_class> personList = new ArrayList<>();
        //查找老师的相关信息
        User_class teacherInformation = classInfo.findFirstByClassnode(classnode);
        List<Student> studentList = ExcelHandler.importExcel(path,1,1,Student.class);
        if(studentList != null) {
            for (Student data : studentList) {
                User_class user_class = new User_class();
                user_class.setName(data.getName());
                Student student = studentInfo.findFirstByStudentnumber(data.getStudentnumber());
                if (student != null) {
                    user_class.setUsername(student.getUsername());
                    user_class.setClassnode(classnode);
                    user_class.setClassname(classname);
                    user_class.setUsertype("学生");
                    user_class.setClassdesn(teacherInformation.getClassdesn());
                    user_class.setTeachername(teacherInformation.getTeachername());
                    user_class.setClassnumber(teacherInformation.getClassnumber());
                }
                personList.add(user_class);
            }
        }
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+personList.size()+"】行");

        return personList;

        //TODO 保存数据库

    }
}
